﻿using System;
using System.Linq;
using NHibernate.Linq;
using SellAndBuy.Domain.Entities;
using SellAndBuy.Domain.Repositories;
using SellAndBuy.Infrastructure.Data.Core;
using SellAndBuy.Infrastructure.Data.Core.Repositories;

namespace SellAndBuy.Infrastructure.Data.Repositories
{
    /// <summary>
    /// Ad Repository
    /// </summary>
    public class AdRepository : Repository<Ad, int>, IAdRepository
    {

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="unitOfWork"></param>
        public AdRepository(INhibernateUnitOfWork unitOfWork)
            : base(unitOfWork)
        {
        }

        /// <summary>
        /// Eager fetch of pics to avoid N+1 select problem
        /// </summary>
        /// <returns>Ad</returns>
        public override Ad FindBy(System.Linq.Expressions.Expression<Func<Ad, bool>> query)
        {
            return _unitOfWork.Session.Query<Ad>().Where(query).Fetch(ad => ad.Pictures).SingleOrDefault();
        }
    }
}